package com.card.client.dialog;

import com.card.client.entry.UserInfo;
import com.card.client.entry.Util;
import com.card.client.service.UserInfoManageService;
import com.card.client.service.UserInfoManageServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class ChangeUserInfoDialog extends DialogBox {

	private static ChangeUserInfoDialogUiBinder uiBinder = GWT
			.create(ChangeUserInfoDialogUiBinder.class);

	interface ChangeUserInfoDialogUiBinder extends
			UiBinder<Widget, ChangeUserInfoDialog> {
	}

	private final UserInfoManageServiceAsync uims = GWT.create(UserInfoManageService.class);
	
	@UiField Button submit;
	
	@UiField Button closeDialog;
	
	@UiField TextBox name;
	
	@UiField TextBox loginName;
	
	@UiField PasswordTextBox pass;
	
	@UiField PasswordTextBox newPass;
	
	@UiField PasswordTextBox newPassSe;
	
	@UiField Label descript;
	
	private UserInfo userInfo;
	
	private UserInfo[] userList;
	
	public ChangeUserInfoDialog(String title, String desc, UserInfo userInfo) {
		setWidget(uiBinder.createAndBindUi(this));
		setTitle(title);
		setText(desc);
		setGlassEnabled(true);
		setAnimationEnabled(true);
		setModal(true);
		descript.setStyleName("descript");
		name.setMaxLength(Util.INPUT_TEXT_MAX_LENGTH);
		loginName.setMaxLength(Util.INPUT_TEXT_MAX_LENGTH);
		pass.setMaxLength(Util.INPUT_TEXT_MAX_LENGTH);
		newPass.setMaxLength(Util.INPUT_TEXT_MAX_LENGTH);
		newPassSe.setMaxLength(Util.INPUT_TEXT_MAX_LENGTH);
		center();
		this.userInfo = userInfo;
		uims.getAllUserInfo(new AsyncCallback<UserInfo[]>() {
			public void onSuccess(UserInfo[] result) {
				userList = result;
			}
			public void onFailure(Throwable caught) {}
		});
		setContent();
	}

	private void setContent() {
		name.setText(userInfo.getName());
		loginName.setText(userInfo.getLoginName());
		loginName.setEnabled(false);
	}

	@UiHandler("submit")
	void onClickSubmit(ClickEvent e) {
		if(!isValid()){
			return;
		}
		final UserInfo ui = new UserInfo();
		ui.setId(userInfo.getId());
		ui.setName(name.getText());
		ui.setLoginName(userInfo.getLoginName());
		ui.setPass(newPass.getText());
		ui.setLastLoginTime(userInfo.getLastLoginTime());
		ui.setLastLoginAddress(userInfo.getLastLoginAddress());
		ui.setCompetence(userInfo.getCompetence());
		userInfo = ui;
		uims.refreshLoginedUserList(ui, new AsyncCallback<Void>() {
			public void onSuccess(Void result) {}
			public void onFailure(Throwable caught) {}
		});
		uims.updateUserInfo(ui, new AsyncCallback<Void>() {
			public void onSuccess(Void result) {
				Window.alert("资料修改成功！");
				hide();
			}
			public void onFailure(Throwable caught) {}
		});
		uims.setLoginedUser(ui, new AsyncCallback<Void>() {
			public void onSuccess(Void result) {}
			public void onFailure(Throwable caught) {}
		});
	}

	@UiHandler("closeDialog")
	void onClickCloseDialog(ClickEvent e) {
		hide();
	}
	
	private void setMessage(String message){
		descript.setText(message);
	}
	
	private boolean isValid(){
		setMessage("");
		String check = Util.validInput(name.getText());
		if(null != check){
			name.setFocus(true);
			setMessage("用户名" + check);
			return false;
		}
		
		check = Util.validInput(loginName.getText());
		if(null != check){
			loginName.setFocus(true);
			setMessage("登录名" + check);
			return false;
		}
		
		check = Util.validInput(pass.getText());
		if(null != check){
			pass.setFocus(true);
			setMessage("密码" + check);
			return false;
		}
		
		check = Util.validInput(newPass.getText());
		if(null != check){
			pass.setFocus(true);
			setMessage("新密码" + check);
			return false;
		}
		
		check = Util.validInput(newPassSe.getText());
		if(null != check){
			pass.setFocus(true);
			setMessage("确认密码" + check);
			return false;
		}
		
		if(!pass.getText().equals(userInfo.getPass())){
			pass.setFocus(true);
			setMessage("原密码输入不正确，请重新输入！");
			resetPass();
			return false;
		}
		
		if(!newPass.getText().equals(newPassSe.getText())){
			pass.setFocus(true);
			setMessage("新密码与确认密码不一致，请重新输入！");
			resetPass();
			return false;
		}
		
		if(userList.length>0){
			for(int i=0;i<userList.length;i++){
				UserInfo ui = userList[i];
				if(ui.getId().equals(userInfo.getId())){
					return true;
				}
				if(ui.getName().equals(name.getText())){
					name.setFocus(true);
					setMessage("用户名已经存在，请重新输入！");
					return false;
				}
			}
		}
		return true;
	}

	private void resetPass() {
		pass.setText("");
		newPass.setText("");
		newPassSe.setText("");
	}
}
